<template>
  <div>
    <el-card shadow="never">
      <el-button icon="el-icon-document-add" type="primary" @click="handleAdd"
        >添加</el-button
      >
    </el-card>

    <el-table
      :data="tableData"
      border=""
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column label="Id">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="轮播图图片">
        <template slot-scope="scope">
          <img
            :src="localhostPath + scope.row.carouselPicturePath"
            alt=""
            width="250px"
            height="170px"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            @change="change(scope.$index, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDelete(scope.$index, scope.row)"
          >
            <el-button size="mini" type="danger" slot="reference"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <el-dialog
      title="轮播图信息"
      :visible.sync="dialogFormVisible"
      width="700px"
    >
      <el-form :model="form">
        <el-form-item label="轮播图" :label-width="formLabelWidth">
          <!-- 上传 -->
          <div class="editor-after-div">
            <el-upload
              :class="{ hide: hideUpload }"
              ref="upload"
              action="#"
              accept="image/png,image/gif,image/jpg,image/jpeg"
              list-type="picture-card"
              :limit="1"
              :http-request="handleUploadAvatar"
              :auto-upload="false"
              :before-upload="handleBeforeUpload"
              :on-change="handleChange"
              :on-remove="handleRemove"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import common from "../utils/common";
import {
  addCarousel,
  getAllPicture,
  IsEnableCarPicture,
  deleteCarpicture,
  editCarPicture,
} from "../api/web";
export default {
  data() {
    return {
      localhostPath: common.localhostPath,
      //上传
      dialogFormVisible: false,
      tableData: [],
      form: {
        carouselPicturePath: "",
        isEnable: true,
      },
      formLabelWidth: "120px",
      //上传一张图片后隐藏上传框
      hideUpload: false,
      limitCount: 1,
      newIndex: "",
    };
  },
  methods: {
    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.form = row;
      this.newIndex = index;
      this.hideUpload = false;

    },
    handleDelete(index, row) {
      console.log(index, row);
      deleteCarpicture(row.id).then(({ data }) => {
        this.tableData.splice(index,1);
        this.$message.success(data.msg);
      });
    },
    //添加
    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {};
      this.hideUpload = false;
      
    },
    //上传
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.hideUpload = false;
    },
    //确定
    confirm() {
      this.$refs.upload.submit();
    },
    handleBeforeUpload(file) {
      console.log("before");
      if (
        !(
          file.type === "image/png" ||
          file.type === "image/gif" ||
          file.type === "image/jpg" ||
          file.type === "image/jpeg"
        )
      ) {
        this.$notify.warning({
          title: "警告",
          message:
            "请上传格式为image/png, image/gif, image/jpg, image/jpeg的图片",
        });
      }
      let size = file.size / 1024 / 1024 / 2;
      if (size > 2) {
        this.$notify.warning({
          title: "警告",
          message: "图片大小必须小于2M",
        });
      }
    },
    handleChange(file) {
      this.hideUpload = true;
      this.dialogImageUrl = file.url;
      console.log(file.url);
    },
    handleUploadAvatar(file) {
      if (this.form.id == undefined) {
        // 添加
        const fileObj = file.file;
        const form1 = new FormData();
        form1.append("imageFile", fileObj);

        addCarousel(form1).then(({ data }) => {
          this.tableData.push(data.data);
        });
        this.dialogFormVisible = false;
        this.$refs.upload.clearFiles();
      } else {
        //编辑
        const fileObj = file.file;
        const form1 = new FormData();
        form1.append("imageFile", fileObj);
        editCarPicture(this.form.id, form1).then(({ data }) => {
          this.tableData.splice(this.newIndex, 1, data.data);
          this.dialogFormVisible = false;
        });
        this.$refs.upload.clearFiles();
      }
    },
    //是否启用
    change(index, row) {
      console.log(index, row);
      this.form.isEnable = row.isEnable;
      IsEnableCarPicture(row.id, this.form).then(({ data }) => {
        console.log(data);
      });
    },
  },
  mounted() {
    getAllPicture().then(({ data }) => {
      this.tableData = data.data;
    });
  },
};
</script>

<style scoped>
.editor-after-div >>> .el-upload {
  width: 500px;
  height: 350px;
  text-align: center;
  line-height: 350px;
}

.editor-after-div >>> .el-upload-list__item-thumbnail {
  width: 500px;
  height: 350px;
}
.editor-after-div >>> .el-upload-list__item {
  width: 500px;
  height: 350px;
}
.avatar {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  display: block;
}
</style>
<style>
.hide .el-upload--picture-card {
  display: none;
}
</style>